package jataaka.parser;

import jataaka.command.URLCreatorCommand;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 
 * Parser for URL information that is retrived from compounded URLs
 * 
 * @author Juan Carlos Cancela
 *
 */
public class URLParser 
{
	/**
	 * parse the url
	 * @param c the command that contains the information to create URLs that will be parsed
	 * @param p the required parsed service to parse it
	 * @return the arraylist of hashed maps with parsed information
	 * @throws IOException
	 */
	public static ArrayList<ArrayList<HashMap<String, String>>> parse(URLCreatorCommand c, ParserService p) throws IOException
	{
		ArrayList<ArrayList<HashMap<String, String>>> parsedResult = new ArrayList<ArrayList<HashMap<String, String>>>();
		for(String url : c.retrieveListOfCompositableURLs()){
			parsedResult.add(p.retrieveTransformedInfo(url));
		}
		return parsedResult;
	}
	
	
	/**
	 * utility to print and retrieve a map
	 * @param map the map to be dumped
	 */
	public static String dumpMap(ArrayList<ArrayList<HashMap<String, String>>> parsedResult) {
		String result = null;
		for(ArrayList<HashMap<String, String>> lh : parsedResult){
			for(HashMap<String, String> mp : lh){
			    Iterator it = mp.entrySet().iterator();
			    while (it.hasNext()) {
			        Map.Entry pairs = (Map.Entry)it.next();
			        System.out.println(pairs.getKey() + " = " + pairs.getValue());
			        result.concat(pairs.getKey() + " = " + pairs.getValue());
			    }
			    System.out.println("\n");
			    result.concat("\n");
			    System.out.println("\n===============");
			    result.concat("\n===============");
			    System.out.println("\n");
			    result.concat("\n");
			}
		}
		return result;
	}
	
}
